<?php
class cls_serve_productdetail extends cls_serve_base
{
	function check_login()
	{
		$user_name=$_SESSION['username'];
		if (!empty($user_name))
		{
			return true;
		}
		else
			return false;
	}
	
	function get_product_original_price($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info = $product_data->get_product_info($product_id);
		$product_original_price = $product_info['Price'];
		return $product_original_price;
	}
	
	function get_product_name($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info = $product_data->get_product_info($product_id);
		$product_name = $product_info['Name'];
		return $product_name;
	}
	
	function get_product_description($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_description=$product_info["Description"];
		return $product_description;
	}
	
	function get_product_image($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_image=$product_info["IMAGE"];
		return $product_image;
	}
	
	function get_product_publisher($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_publisher=$product_info["Publisher"];
		return $product_publisher;
	}
	
	function get_product_language($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_language=$product_info["Productanguage"];
		return $product_language;
	}
	
	function get_product_author($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_author=$product_info["Author"];
		return $product_author;
	}
	
	function get_product_quantity($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_quantity=$product_info["Quantity"];
		return $product_quantity;
	}
	
	function get_product_stock($product_id)
	{
		$quantity=$this->get_product_quantity($product_id);
		if ($quantity>0)
		{
			return 1;
		}
		else
			return 0;
	}
	
	function get_product_totalpage($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_totalpage=$product_info["TotalPage"];
		return $product_totalpage;
	}
	
	
	function get_product_discount($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$discount_info = $product_data->get_product_discount($product_id);
		$discount_type=$discount_info['DiscountType'];
		$discount_amount=$discount_info['Amount'];
		if($discount_info['Status']==1)
		{
			$discount=array("Type"=>$discount_type,"Amount"=>$discount_amount);
		}		
		return $discount;
	}
	
/*	function get_product_current_price($product_id)
	{
		$original_price=$this->get_product_original_price($product_id);
		$product_data = cls_data_base::get_instance($this->name);
		$discount_info = $product_data->get_product_discount($product_id);
		$discount_type=$discount_info['Type'];
		$discount_amount=$discount_info['Quantity'];
		$discount_status=$discount_info['Status'];
		if ($discount_status!=1)
		{
			return $original_price;
		}
		else
		{
			if($discount_type=='2')
			{
				return $original_price-$discount_amount;
			}
			else 
				return $original_price*$discount_amount;
		}		
	}
*/
	function get_current_price($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$discount_info=$product_data->get_product_discount($product_id);
		if ($discount_info['DiscountType']==1&&$discount_info['Status']==1)
		{
			$current_price=$product_info['Price']*$discount_info['Amount'];
	        return $current_price;
		}
		
		if ($discount_info['DiscountType']==2&&$discount_info['Status']==1)
		{
			$current_price=$product_info['Price']-$discount_info['Amount'];
	        return $current_price;
		}
		
		if ($discount_info['DiscountType']==3&&$discount_info['Status']==1)
		{
			$current_price=$product_info['Price']*$discount_info['Amount'];
			return $current_price;
		}
		else
			return $product_info['Price'];
	}
	
	function get_favorites($user_name,$product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$favorites=$product_data->get_favorites($user_name,$product_id);
		if(empty($favorites['ID']))
			return 0;
		else
			return 1;
	}
	
	function set_favorites($user_name,$product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_data->set_favorites($user_name,$product_id);
	}
	
	function get_comment($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_comment=$product_info["productcomment"];
		return $product_comment;
	}
	
	function get_description($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$product_description=$product_info["Description"];
		return $product_description;
	}
	
	function get_product_info($product_id)
	{
		$product_data=cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		
		return $product_info;
	}
	
	function get_saved_proportion($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$saved_proportion=10*$this->get_current_price($product_id)/$product_info['Price'];
		return $saved_proportion;
	}
	
	function get_saved_value($product_id)
	{
		$product_data = cls_data_base::get_instance($this->name);
		$product_info=$product_data->get_product_info($product_id);
		$saved_value=$product_info['Price']-$this->get_current_price($product_id);
		return $saved_value;
	}
	
}


